package com.example.leadnews.user.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.crypto.digest.BCrypt;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.leadnews.user.dto.LoginDTO;
import com.example.leadnews.user.dto.RegisterDTO;
import com.example.leadnews.user.entity.User;
import com.example.leadnews.user.service.UserService;
import com.wuyunbin.leadnews.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author wuyunbin
 * @since 2023-03-23
 */
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserService userService;

    @GetMapping("checkUsername/{username}")
    public boolean checkUsername(@PathVariable String username) {
        //查询条件拼接 相当于 sql where那部分内容 where username=?
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername, username);

        User user = userService.getOne(wrapper);
        log.info("{}", user);

//        if(user==null){
//            return false;
//        }else{
//            return true;
//        }

        return user != null;
    }

    @PostMapping("register")
    public boolean register(@RequestBody RegisterDTO registerDTO) {

        User user = new User();
        //传递的是手机号
        if (!("".equals(registerDTO.getPhone()) || registerDTO.getPhone() == null)) {
            user.setPhone(registerDTO.getPhone());
        }

        //传递的是邮箱
        if (!("".equals(registerDTO.getEmail()) || registerDTO.getEmail() == null)) {
            user.setEmail(registerDTO.getEmail());
        }

        //传递的是用户名
        if (!("".equals(registerDTO.getUsername()) || registerDTO.getUsername() == null)) {
            user.setUsername(registerDTO.getUsername());
        }

        /*
        明文 -> md5 彩虹表攻击 淘汰
        明文->密文 ->引入 ss  or  引入hutool
         */
        user.setPassword(BCrypt.hashpw(registerDTO.getPassword()));

        boolean result = userService.save(user);

        return result;
    }

    @PostMapping("login")
    public Result<String> login(@RequestBody LoginDTO loginDTO) {
        User userInDb = null;
        //phone
        if (!(loginDTO.getPhone() == null || "".equals(loginDTO.getPhone()))) {
            LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(User::getPhone, loginDTO.getPhone());
            userInDb = userService.getOne(wrapper);
        }
        //email
        if (!(loginDTO.getEmail() == null || "".equals(loginDTO.getEmail()))) {
            LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(User::getEmail, loginDTO.getEmail());
            userInDb = userService.getOne(wrapper);
        }
        //用户名
        if (!(loginDTO.getUsername() == null || "".equals(loginDTO.getUsername()))) {
            LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(User::getUsername, loginDTO.getUsername());
            userInDb = userService.getOne(wrapper);
        }
        //比对密码 前面是传进来的明文 后面是数据库读取的密文
        boolean checkpw = BCrypt.checkpw(loginDTO.getPassword(), userInDb.getPassword());
        log.info("{}",checkpw);

        //处理登录后的操作->创建token
        StpUtil.login(userInDb.getId());
        String token = StpUtil.getTokenInfo().getTokenValue();

        log.info(token);
        redisTemplate.opsForValue().set(token,1,200, TimeUnit.SECONDS);
        return Result.success(token);
    }

    @PostMapping("findByToken")
    public void findByToken(){
        log.info("findByToken");
    }
}
